package com.zjsru.plan2024.oneday;

/**
 * @Author: cookLee
 * @Date: 2024-01-15
 * 删除排序链表中的重复元素 II
 */
public class DeleteDuplicates2 {

    /**
     * 主
     * \
     * 输入：head = [1,2,3,3,4,4,5]
     * 输出：[1,2,5]
     * \
     * @param args args
     */
    public static void main(String[] args) {
        DeleteDuplicates2 deleteDuplicates2 = new DeleteDuplicates2();
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(3, new ListNode(4, new ListNode(4, new ListNode(5)))))));
        System.out.println(deleteDuplicates2.deleteDuplicates(head));
    }

    /**
     * 删除重复项
     *
     * @param head 封头
     * @return {@link ListNode}
     */
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode ans = new ListNode(0, head);
        ListNode curr = ans;
        while (curr.next != null && curr.next.next != null) {
            if (curr.next.val == curr.next.next.val) {
                int currValue = curr.next.val;
                while (curr.next != null && curr.next.val == currValue) {
                    curr.next = curr.next.next;
                }
            } else {
                curr = curr.next;
            }
        }
        return ans.next;
    }

}
